<?php
/**
 * Created By virus_cms
 * Author: Virus
 * Date: 2020/3/2
 * Time: 17:10
 */

namespace app\common\model\mysql;


use think\Model;

class AuthRule extends Model
{
    public function getTypeList()
    {
        return [
            '1' => "<span class=\"label label-primary\">模块</span>",
            '2' => "<span class=\"label label-success\">操作</span>",
        ];
    }

    public function getTypeAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list  = $this->getTypeList();

        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusAttr($value)
    {
        return $value ? "<span class='label label-success'>开启</span>" : "<span class='label label-danger'>禁用</span>";
    }

    public function getIsMenuAttr($value)
    {
        return $value ? '是' : '否';
    }

    /**
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAllTopPermissions()
    {
        return $this->select();
    }

    public function addRule($data)
    {
        if (empty($data) || !is_array($data)) {
            return false;
        }

        return $this->save($data);
    }

    public function getOneRuleById($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return false;
        }

        $where = ['id' => $id];

        return $this->where($where)->find()->getData();
    }

    public function updateRuleById($id, $data)
    {
        $id = intval($id);
        if (empty($id) || empty($data) || !is_array($data)) {
            return false;
        }

        $where = ['id' => $id];

        return $this->where($where)->save($data);
    }

    public function getSonRuleByPidEquealId($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return false;
        }

        $where = ['pid' => $id];

        return $this->where($where)->count();
    }

    public function deleteById($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return false;
        }
        $where = [
            'id' => $id,
        ];

        return $this->where($where)->delete();
    }
}